package com.lagou.springbootzy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2020/11/6
 */
@Controller
public class IndexController {


    @Autowired
    private ArticleDao articleDao;
    @GetMapping("/")
    public String index(Model model)
    {
        Page<Article> all = articleDao.findAll(PageRequest.of(0,3));
        model.addAttribute("articles",all.getContent());
        model.addAttribute("total",all.getTotalPages());
        model.addAttribute("prev",0);
        model.addAttribute("next",1);
        model.addAttribute("cur",all.getNumber());
        model.addAttribute("allnum",all.getTotalPages());
        return "index";
    }

    @GetMapping("/page/{num}")
    public String page(@PathVariable("num")Integer num,Model model)
    {
        Page<Article> all = articleDao.findAll(PageRequest.of(num,3));
        model.addAttribute("articles",all.getContent());
        model.addAttribute("total",all.getTotalPages());
        model.addAttribute("prev",0);
        model.addAttribute("next",1);
        model.addAttribute("allnum",all.getTotalPages());
        return "index";
    }
}
